package mail;

import java.util.Properties;
import java.util.Vector;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class SndMail
{
	private static final String SMTP_HOST_NAME = "mail.osc.co.in";
	Vector<Object> to = new Vector<Object>();
	
	public boolean postMail(String from, Vector<Object> toAdd,String fileAttachment,String subject,String bodyText) 
	{
		try
		{
			to.addAll(toAdd);
			 //String host = "osc.co.in";
			 //String from = "mahendra@osc.co.in";
			 //String to = "maruteeg@osc.co.in";
			 //String fileAttachment = "c://1.txt";
			 Properties props = System.getProperties();

			 props.put("mail.smtp.host", SMTP_HOST_NAME);
			 props.put("mail.Transport.protocol","smtp");
			 props.put("mail.smtp.port","25");
			 props.put("mail.smtp.auth","true");
//			 Setup mail server
			    Authenticator auth = new PopupAuthenticator();
			    Session session =  Session.getInstance(props, auth);
			 try
			 {
				MimeMessage message =    new MimeMessage(session);
			    message.setFrom( new InternetAddress(from));
			    for(int i=0;i<to.size();i++)
				{
			    	
			    	String add = null;
					try
					{
						add = to.get(i).toString();
						//System.out.println("Address :"+add);
						message.addRecipient(Message.RecipientType.TO,new InternetAddress(add));
					}
					catch (Exception e)
					{
						//e.printStackTrace();
						System.out.println(add+" Adress not found");
					}
				}
			    Address[]mim = message.getRecipients(Message.RecipientType.TO);
			    for(int i=0;i<mim.length;i++)
			    	System.out.println(mim[i].toString());
			    
			    message.setSubject(subject);
			    MimeBodyPart messageBodyPart =  new MimeBodyPart();
			    messageBodyPart.setText(bodyText);
			    Multipart multipart = new MimeMultipart();
			    multipart.addBodyPart(messageBodyPart);
			    messageBodyPart = new MimeBodyPart();
			    DataSource source = new FileDataSource(fileAttachment);
			    messageBodyPart.setDataHandler(new DataHandler(source));
			    messageBodyPart.setFileName(fileAttachment);
			    multipart.addBodyPart(messageBodyPart);
			    message.setContent(multipart);
			    Transport.send(message);
			    System.out.println("After Mail Send");
			    return true;
		    }
		    catch(Exception e)
		    {
		    	e.printStackTrace();
		    }
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		return false;
	 }
	public static void main(String[] args) 
	{
		SndMail send = new SndMail();
		Vector<Object> vect = new Vector<Object>();
		vect.add("maruteeg@oscpro.in");
		vect.add("osradmin@gmail.com");
		//vect.add("fcmurray@gmail.com");
		send.postMail("bills@osc.co.in",vect , "D:\\Reports\\BillReports\\reportupper286_mail.txt", "Testing Mail", "Hi.. this is testing please ignore this message");
	}
	
	static class PopupAuthenticator extends Authenticator {
		
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("bills@osc.co.in", "billsosc");
        }
    }

}
